package alex.edu;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class ServerResponse {

	ArrayList<Country> countries_user = new ArrayList<Country>();
	ArrayList<Country> country_user = new ArrayList<Country>();
	int access_time;

	public void getAllStatus(String path) {
		// Get the XML
		URL url;
		try {
			String statusFeed = path;
			url = new URL(statusFeed);
			URLConnection connection;
			connection = url.openConnection();
			HttpURLConnection httpConnection = (HttpURLConnection) connection;
			int responseCode = httpConnection.getResponseCode();
			if (responseCode == HttpURLConnection.HTTP_OK) {
				InputStream in = httpConnection.getInputStream();
				DocumentBuilderFactory dbf;
				dbf = DocumentBuilderFactory.newInstance();
				DocumentBuilder db = dbf.newDocumentBuilder();
				// Parse the earthquake feed.
				Document dom = db.parse(in);
				Element docEle = dom.getDocumentElement();
				// Get a list of each earthquake entry.
				NodeList country_list = docEle.getElementsByTagName("country");
				NodeList users_list;
				Country current_country;
				// // clean the list of countries
				// countries_user.removeAll(countries_user);
				if (country_list != null && country_list.getLength() > 0) {
					for (int i = 0; i < country_list.getLength(); i++) {
						Element country = (Element) country_list.item(i);
						String country_name = (country.getAttributeNode("name"))
								.getValue();
						current_country = new Country(country_name);
						users_list = country.getElementsByTagName("user");
						for (int j = 0; j < users_list.getLength(); j++) {
							Element user = (Element) users_list.item(j);
							// get 'firstname' country element
							String firstName = ((Element) user
									.getElementsByTagName("firstname").item(0))
									.getFirstChild().getNodeValue();
							// get 'lastname' country element
							String lastName = ((Element) user
									.getElementsByTagName("lastname").item(0))
									.getFirstChild().getNodeValue();
							// get 'status' country element
							String status = ((Element) user
									.getElementsByTagName("status").item(0))
									.getFirstChild().getNodeValue();
							// create new user
							User_Map user_map = new User_Map(status, firstName,
									lastName);
							// add user to the specifeid country name
							current_country.add_user(user_map);
						}
						this.countries_user.add(current_country);
					}
				}
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} finally {
		}
	}

	/*
	 * Country getCountry(Element countryNode){ return null; }
	 */
}
